package android.slc.appbase.repository.remote;

import android.slc.appbase.R;
import android.slc.appbase.api.slc.callback.po.SlcEntity;
import android.slc.appbase.api.slc.config.ApiConfig;
import android.slc.appbase.api.slc.converter.SlcEntityErrorException;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.functions.Function;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/20 上午 11:25
 */
public class DataTransformUtils {

    public static <R> SlcEntity<R> slcEntity2TargetRSlcEntity(SlcEntity<?> input) {
        SlcEntity<R> pageModelSlcEntity = new SlcEntity<>();
        pageModelSlcEntity.setCode(input.getCode());
        pageModelSlcEntity.setMsg(input.getMsg());
        return pageModelSlcEntity;
    }

    /**
     * 检查空
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<T>, SlcEntity<T>> checkNull() {
        return checkNull(StringUtils.getString(R.string.label_data_is_null));
    }

    /**
     * 检查空
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<T>, SlcEntity<T>> checkNull(String errorMsg) {
        return dataSlcEntity -> {
            if (dataSlcEntity.isSuccess() && dataSlcEntity.getData() == null) {
                throw new SlcEntityErrorException(ApiConfig.Value.A_KNOWN_ERROR, errorMsg);
            }
            return dataSlcEntity;
        };
    }

    /**
     * 检查空
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<T>, SlcEntity<T>> checkEmpty() {
        return checkEmpty(StringUtils.getString(R.string.label_data_is_null));
    }

    /**
     * 检查空
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<T>, SlcEntity<T>> checkEmpty(String errorMsg) {
        return dataSlcEntity -> {
            if (dataSlcEntity.isSuccess()) {
                if (dataSlcEntity.getData() == null) {
                    throw new SlcEntityErrorException(ApiConfig.Value.A_KNOWN_ERROR, errorMsg);
                } else if (dataSlcEntity.getData() instanceof List<?>) {
                    if (CollectionUtils.isEmpty((List<?>) dataSlcEntity.getData())) {
                        throw new SlcEntityErrorException(ApiConfig.Value.A_KNOWN_ERROR, errorMsg);
                    }
                }
            }
            return dataSlcEntity;
        };
    }

    /**
     * list item直接转目标类型 list Item
     *
     * @param dataList
     * @param <T>
     * @return
     */
    public static <T, R> List<R> itemTransitionTargetList(List<T> dataList) {
        return itemTransitionTargetList(dataList, null);
    }

    /**
     * list item直接转目标类型 list Item
     *
     * @param dataList
     * @param transitionSelectBoxDisturb
     * @param <T>
     * @return
     */
    public static <T, R> List<R> itemTransitionTargetList(List<T> dataList, TransitionDisturb<T> transitionSelectBoxDisturb) {
        List<R> targetList = new ArrayList<>();
        CollectionUtils.forAllDo(dataList, (index, item) -> {
            if (transitionSelectBoxDisturb != null && !transitionSelectBoxDisturb.evaluate(item)) {
                return;
            }
            targetList.add((R) item);
        });
        return targetList;
    }

    /**
     * list item转list selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T, R> Function<SlcEntity<List<T>>, SlcEntity<List<R>>> transitionTargetListMap() {
        return transitionTargetListMap(null);
    }

    /**
     * list item转list selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T, R> Function<SlcEntity<List<T>>, SlcEntity<List<R>>> transitionTargetListMap(TransitionDisturb<T> transitionSelectBoxDisturb) {
        return new Function<SlcEntity<List<T>>, SlcEntity<List<R>>>() {
            @Override
            public SlcEntity<List<R>> apply(@NotNull SlcEntity<List<T>> listSlcEntity) {
                List<T> dataList = listSlcEntity.getData();
                if (listSlcEntity.isSuccess() && dataList == null) {
                    dataList = new ArrayList<>();
                    listSlcEntity.setData(dataList);
                }
                SlcEntity<List<R>> listSelectBoxSlcEntity = new SlcEntity<>();
                listSelectBoxSlcEntity.setStatus(listSlcEntity.getStatus());
                listSelectBoxSlcEntity.setMsg(listSlcEntity.getMsg());
                listSelectBoxSlcEntity.setData(itemTransitionTargetList(dataList, transitionSelectBoxDisturb));
                return listSelectBoxSlcEntity;
            }
        };
    }

    /**
     * item转selectBox选择状态干扰器
     */
    public interface TransitionDisturb<E> {
        boolean evaluate(E item);
    }
}